package idv.cjcat.pyronova.display {
	import flash.display.BitmapData;
	import idv.cjcat.pyronova.nodes.Node;
	import idv.cjcat.pyronova.render.Graphics;
	import idv.cjcat.pyronova.render.RenderVisitor;
	import idv.cjcat.pyronova.pyro;
	
	public class PyroView {
		
		public var autoBufferLock:Boolean;
		
		private var _bitmapData:BitmapData;
		public final function bitmapData():BitmapData { return _bitmapData; }
		
		public var root:Node;
		public var preprocessor:Node;
		public var postprocessor:Node;
		
		private var _visitor:RenderVisitor;
		
		public final function graphics():Graphics { return _visitor.graphics(); }
		
		private var _width:Number;
		public function width():Number { return _width; }
		
		private var _height:Number;
		public function height():Number { return _height; }
		
		public function PyroView(width:Number = 640, height:Number = 400, autoBufferLock:Boolean = true) {
			_width = (width > 0)?(width):(1);
			_height = (height > 0)?(height):(1);
			_bitmapData = new BitmapData(_width, _height, true, 0x00000000);
			_visitor = new RenderVisitor(_width, _height, new Graphics(_bitmapData));
			
			this.autoBufferLock = autoBufferLock;
		}
		
		public final function render():void {
			var graphics:Graphics = _visitor.graphics();
			
			_visitor.pyro::init();
			if (autoBufferLock) graphics.lockBuffers();
			
			if (preprocessor) preprocessor.render(_visitor);
			if (root) root.render(_visitor);
			if (postprocessor) postprocessor.render(_visitor);
			
			if (autoBufferLock) graphics.unlockBuffers();
			_visitor.pyro::finish();
		}
	}
}